<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     个人中心.
 * @Author          Evelyn丶好久不见
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2019/4/28
 */
namespace app\api\controller;
use app\common\controller\CommonApi;
use think\Db;
class Address extends CommonApi{
//收货列表
    public function addresslist(){
        $list = db('address')->where(['user_id'=>UID])->order('is_choose desc')->limit(($this->p-1)*10,10)->select();
        foreach ($list as $key => $value) {
            $list[$key]['all_address'] = $value['province'].$value['city'].$value['county'].$value['address'];
        }
        $this->apireturn(1,'收货地址',$list);
    }

    public function chooseadd(){
        $add = db('address')->where(['user_id'=>UID,'is_choose'=>1])->find();
        if($add){
            $add['all_address'] = $add['province'].$add['city'].$add['county'].$add['address'];
        }
        $this->apiReturn(1,'默认收货地址',$add);
    }

//添加收货地址
    public function add_address(){
        $choose = input('is_choose',0);
        if($choose == 1){
            db('address')->where(['user_id'=>UID])->update(['is_choose'=>0]);
        }

        $query = db('address')->insert(['user_id'=>UID,'province'=>input('province'),'city'=>input('city'),'county'=>input('county'),'address'=>input('address'),'phone'=>input('phone'),'name'=>input('name'),'is_choose'=>input('is_choose',0)]);
        if(!$query){
            $this->apiReturn(0,'操作失败');
        }
        $this->apiReturn(1,'操作成功');
    }

//修改收货地址
    public function edit_address(){
        $choose = input('is_choose',0);
        if($choose == 1){
            db('address')->where(['user_id'=>UID])->update(['is_choose'=>0]);
        }
        $info = db('address')->where(['id'=>input('id',0),'user_id'=>UID])->find();
        if(!$info)
            $this->apiReturn(0,'信息错误');

        $query = db('address')->where(['id'=>input('id'),'user_id'=>UID])->update(['province'=>input('province'),'city'=>input('city'),'county'=>input('county'),'address'=>input('address'),'phone'=>input('phone'),'name'=>input('name'),'is_choose'=>input('is_choose',0),'update_time'=>time(),'update_time'=>time()]);
        if(!$query){
            $this->apiReturn(0,'操作失败');
        }
        $this->apiReturn(1,'操作成功');
    }

//删除收货地址
    public function del_address(){
        if(db('address')->where(['id'=>input('id'),'user_id'=>UID])->find() == ''){
            $this->apiReturn(0,'信息错误');
        }
        $query = db('address')->where(['id'=>input('id'),'user_id'=>UID])->delete();
        if(!$query)
            $this->apiReturn(0,'操作失败');
        $this->apiReturn(1,'操作成功');
    }
}